<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/25
 * Time: 15:59
 */

namespace app\index\controller;
use think\Db;


class GiftOrder
{

    public function create1()
    {
        $gift_count_list = Db::name('gift_count')->where('gift_id', 1)->column('single_id,gift_count,gift_count gift_origin_count', 'id');
        $arr = ['order_id' => 1];
        array_walk($gift_count_list, function (&$value, $key, $arr) {
            $value = array_merge($value, $arr);
        }, $arr);
        $ret = Db::name('order_gift_count')->insertAll($gift_count_list);
        //halt($gift_count_list);
    }

    /**
     * @param $id
     * @param $member_id
     * @param int $single_id
     * @param int $bill
     * @param string $bill_type
     * @param int $bill_header
     * @param string $bill_content
     * @param string $bill_email
     * @param string $bill_phone
     * @param string $explain
     * @param int $shipping
     * @param int $pay_way
     * @return array
     * 创建礼包订单
     */
    public function create($id,$member_id,$single_id = 0,$bill = 1,$bill_type = '',$bill_header = 0,$bill_content = '',$bill_email = '',$bill_phone = '',$explain = '',$shipping = 1,$pay_way = 1)
    {
        //sql默认t_type = 1 如果$t_type = 2 就是 就是会员卡
        Db::startTrans();
        try {
            $gift = Db::name('gift')->where('id', $id)->find();
            if($gift['day'] <= 0){
                return ['code' => 400, 'message' => '礼包未设置使用期限'];
            }
            if($gift['sycs'] <=0 ){
                return ['code' => 400, 'message' => '剩余次数不足'];
            }
            $member = Db::name('member')->find($member_id);
            $order_number = order_number();
            $shop_order_number = shop_order_number($member_id);
            $username = Db::name('member')->where('id', $member_id)->value('username');
            $single_title = Db::name('single')->where('id', $single_id)->value('cat_name');
            $order = [
                'order_number' => $order_number,
                'shop_order_number' => $shop_order_number,
                'order_type' => 3,
                'service_type_id' => 0,//1保险2年检3 4个没有附表的分类
                'member_id' => $member_id,
                'username' => $username,
                'amount' => $gift['sale_price'],
                'sale_price' => $gift['sale_price'],
                'gift_origin_price' => $gift['origin_price'],
                'pay_way' => $pay_way,//线上
                'shipping' => $shipping,
                'explain' => $explain,
                'bill' => $bill,//是否开发票
                'bill_type' => $bill_type,//是否开发票
                'bill_header' => $bill_header,//是否开发票
                'bill_content' => $bill_content,//是否开发票
                'bill_email' => $bill_email,//是否开发票
                'bill_phone' => $bill_phone,
                'status' => 1,//未支付-待支付
                'gift_id' => $id,
                'gift_title' => $gift['title'],
                'gift_describe' => $gift['describe'],
                'gift_content' => $gift['content'],
                'gift_start_time' => $gift['start_time'],
                'gift_end_time' => $gift['end_time'],
                'gift_total_count' => $gift['count'],
                'per' => $gift['per'],
                'day' => $gift['day'],
                'use_time' => date('Y-m-d H:i:s',mktime(0,0,0,0,367,0)),
                'create_time' => date('Y-m-d H:i:s', time())
            ];
            $order_id = Db::name('order')->insertGetId($order);

            Db::name('gift')->where('id', $id)->setInc('sale_number',1);//销量
            Db::name('gift')->where('id', $id)->setDec('sycs',1); //剩余次数

            $gift_count_list = Db::name('gift_count')->where('gift_id',$id)->column('single_id,gift_count,gift_count gift_origin_count');
            $arr = ['order_id' => $order_id];
            array_walk($gift_count_list, function (&$value, $key, $arr) {
                $value = array_merge($value, $arr);
            }, $arr);
            $ret = Db::name('order_gift_count')->insertAll($gift_count_list);

            if ($order_id && $ret) {
                Db::commit();
                return ['code' => 200, 'message' => '礼包订单生成成功', 'order_number' => $order_number];
            }

            return ['code' => 400, 'message' => '失败'];

        } catch (\Exception $e) {
            Db::rollback();
            return ['code' => $e->getCode(),'message' => $e->getMessage(),'line' => $e->getLine()];
        }
    }

    /**
     * @param $id
     * @param $member_id
     * @param int $single_id
     * @param int $bill
     * @param string $bill_type
     * @param int $bill_header
     * @param string $bill_content
     * @param string $bill_email
     * @param string $bill_phone
     * @param string $explain
     * @param int $shipping
     * @param int $pay_way
     * @return array
     * 创建会员卡订单
     */
    public function create_member_card($id,$member_id,$single_id = 0,$bill = 1,$bill_type = '',$bill_header = 0,$bill_content = '',$bill_email = '',$bill_phone = '',$explain = '',$shipping = 1,$pay_way = 1)
    {
        //sql默认t_type = 1 如果$t_type = 2 就是 就是会员卡
        Db::startTrans();
        try {
            $gift = Db::name('gift')->where('id', $id)->find();
            if($gift['day'] <= 0){
                return ['code' => 400, 'message' => '礼包未设置使用期限'];
            }
            if($gift['sycs'] <=0 ){
                return ['code' => 400, 'message' => '剩余次数不足'];
            }
            $member = Db::name('member')->find($member_id);
            $order_number = order_number();
            $shop_order_number = shop_order_number($member_id);
            $username = Db::name('member')->where('id', $member_id)->value('username');
            $single_title = Db::name('single')->where('id', $single_id)->value('cat_name');
            $order = [
                'order_number' => $order_number,
                'shop_order_number' => $shop_order_number,
                'order_type' => 3,
                'service_type_id' => 0,//1保险2年检3 4个没有附表的分类
                'member_id' => $member_id,
                'username' => $username,
                'amount' => $gift['sale_price'],
                'sale_price' => $gift['sale_price'],
                'gift_origin_price' => $gift['origin_price'],
                'pay_way' => $pay_way,//线上
                'shipping' => $shipping,
                'explain' => $explain,
                'bill' => $bill,//是否开发票
                'bill_type' => $bill_type,//是否开发票
                'bill_header' => $bill_header,//是否开发票
                'bill_content' => $bill_content,//是否开发票
                'bill_email' => $bill_email,//是否开发票
                'bill_phone' => $bill_phone,
                'status' => 1,//未支付-待支付
                'gift_id' => $id,
                'gift_title' => $gift['title'],
                'gift_describe' => $gift['describe'],
                'gift_content' => $gift['content'],
                'gift_start_time' => $gift['start_time'],
                'gift_end_time' => $gift['end_time'],
                'gift_total_count' => $gift['count'],
                'per' => $gift['per'],
                'day' => $gift['day'],
                'use_time' => date('Y-m-d H:i:s',mktime(0,0,0,0,367,0)),
                'create_time' => date('Y-m-d H:i:s', time())
            ];
            $order_id = Db::name('order')->insertGetId($order);

            Db::name('gift')->where('id', $id)->setInc('sale_number',1);//销量
            Db::name('gift')->where('id', $id)->setDec('sycs',1); //剩余次数

            $gift_count_list = Db::name('gift_count')->where('gift_id',$id)->column('single_id,gift_count,gift_count gift_origin_count');
            $arr = ['order_id' => $order_id]; //对数组中的每个元素使用自定义函数
            array_walk($gift_count_list, function (&$value, $key, $arr) {
                $value = array_merge($value, $arr);
            }, $arr);
            $ret = Db::name('order_gift_count')->insertAll($gift_count_list);

            if ($order_id && $ret) {
                Db::commit();
                return ['code' => 200, 'message' => '礼包订单生成成功', 'order_number' => $order_number];
            }

            return ['code' => 400, 'message' => '失败'];

        } catch (\Exception $e) {
            Db::rollback();
            return ['code' => $e->getCode(),'message' => $e->getMessage(),'line' => $e->getLine()];
        }
    }
}